package com.jrx.anytxn.transaction.job.marketingFile.step;

import com.jrx.anytxn.common.uitl.DateUtils;
import com.jrx.anytxn.customer.constant.CustomerConstant;
import com.jrx.anytxn.customer.mapper.ext.ExtTlMarketingRecordMapper;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Map;

@Service
public class MarketingSumFileProcessor implements ItemProcessor<String, String> {

    @Resource
    private ExtTlMarketingRecordMapper extTlMarketingRecordMapper;
    @Resource
    private IOrganizationTableService organizationTableService;

    @Override
    public String process(String item) throws Exception {
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(TransBizConstant.DEFAULT_ORG_ID);
        //注册
        Map<String, Object> regMap = extTlMarketingRecordMapper.selectSumByBusDateAndRecomderAndStep(org.getToday(), item, CustomerConstant.MARKET_STEP_1);
        //授信
        Map<String, Object> creditMap = extTlMarketingRecordMapper.selectSumByBusDateAndRecomderAndStep(org.getToday(), item, CustomerConstant.MARKET_STEP_2);

        StringBuffer sb = new StringBuffer();
        sb.append("\""+ (DateUtils.format(org.getToday(), DateUtils.DATE_PATTERN_ANOTHER)) + "\""+ TransBizConstant.SPLIT);
        sb.append("\""+ (item==null?"":item) + "\""+ TransBizConstant.SPLIT);
        sb.append("\""+ (regMap.get("count")==null?"0":regMap.get("count")) + "\""+ TransBizConstant.SPLIT);
        sb.append("\""+ (creditMap.get("count")==null?"0":creditMap.get("count")) + "\""+ TransBizConstant.SPLIT);
        sb.append("\""+ (creditMap.get("creditAmount")==null?"0.00":((BigDecimal)creditMap.get("creditAmount")).setScale(2, BigDecimal.ROUND_HALF_UP)) + "\""+ TransBizConstant.SPLIT);
        sb.append("\""+ "" + "\""+ TransBizConstant.SPLIT);
        sb.append("\""+ "" + "\""+ TransBizConstant.SPLIT);
        sb.append("\""+ "" + "\""+ TransBizConstant.SPLIT);
        sb.append("\""+ "" + "\""+ TransBizConstant.SPLIT);
        sb.append("\""+ "" + "\""+ TransBizConstant.SPLIT);
        sb.append("\""+ org.getTenantId()+ "\"");
        return sb.toString();
    }
}
